// pages/teacher/create-notice/index.js
import { Serve } from '../../../utils/index'
Page({
	/**
	 * 页面的初始数据
	 */
	data: {
		classes: [],
		content: '',
		max: 150,
		currentClass: '',
		hasFile: false,
		path: {},
		file: false,
		icon: {
			doc: '/icons/icon-word.svg',
			doc: '/icons/icon-word.svg',
			ppt: '/icons/icon-ppt.svg',
			pptx: '/icons/icon-ppt.svg',
		},
	},

	// mark: 输入框绑定函数
	handleContentInput(e) {
		this.setData({ content: e.detail.value })
	},

	// mark: switch函数
	handleHasFileChange(e) {
		this.setData({ hasFile: e.detail.value })
	},

	// mark: 选择文件
	async chooseFile(e) {
		const isEqualString = (str1, str2) => {
			return str1.toLowerCase() === str2.toLowerCase()
		}
		const result = await wx.chooseMessageFile({
			count: 1,
			type: 'file',
			extensions: ['doc', 'docx', 'ppt', 'pptx'],
		})

		const file = result.tempFiles[0]
		const arr = file.path.split('.')
		// 取后缀
		const suffix = arr[arr.length - 1]
		if (
			!isEqualString(suffix, 'doc') &&
			!isEqualString(suffix, 'docx') &&
			!isEqualString(suffix, 'ppt') &&
			!isEqualString(suffix, 'pptx')
		) {
			wx.showToast({ title: '请上传ppt或word文件', icon: 'error', mask: true })
			return 0
		}

		file.suffix = suffix

		this.setData({ path: file, file: true })
	},

	// "选择"按钮事件
	selectClass(e) {
		const { id } = e.currentTarget.dataset
		const { classes } = this.data
		const index = classes.findIndex((i) => i._id === id)
		const currentClass = classes[index]._id
		this.setData({ index, currentClass })
	},

	async confirmAddNotice() {
		const { currentClass, content, hasFile, file, path } = this.data
		let suffix = ''
		let filename = ''
    let attach = ''
		const openid = wx.getStorageSync('openid')
		if (!content) {
			wx.showToast({ title: '请输入文字内容', mask: true, icon: 'error' })
			return 0
		}
		// 如果选择了"有附件"且未上传附件
		if (hasFile && !file) {
			wx.showToast({ title: '请上传附件', mask: true, icon: 'error' })
			return 0
		}
		if (!currentClass) {
			wx.showToast({ title: '请选择一个班级', mask: true, icon: 'error' })
			return 0
		}

    // 上传文件
		if (hasFile) {
      wx.showLoading({ title: '上传文件', mask: true })
			const uploadResult = await wx.cloud.uploadFile({
				cloudPath: `${openid}/files/${path.name}`,
				filePath: path.path,
			})
			const { fileID } = uploadResult
			const fileResult = await Serve.AddFile({
				path: fileID,
				alias: '',
				class_id: currentClass,
				test_id: '',
				suffix,
				filename,
			})
      attach = fileID
      wx.hideLoading()
		}
    wx.showLoading({ title: '发布公告', mask: true })
		const result = await Serve.AddNotice({ class_id: currentClass, content, attach })
		wx.showToast({ icon: 'success', mask: true, title: '成功' })
		wx.navigateBack()
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		const _this = this
		const role = 2
		wx.showLoading({ title: '获取班级信息', mask: true })
		Serve.GetClasses(role).then((result) => {
			_this.setData({ classes: result })
			wx.hideLoading()
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady() {},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow() {},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide() {},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload() {},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh() {},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom() {},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage() {},
})
